/*******************************************************************************
 * Copyright 2007, 2009 InnovaSoft SA http://www.innovasoft.biz/
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/

package biz.innovasoft.nodb.query;

import java.io.Serializable;
import java.util.List;

import org.apache.lucene.search.SortField;

/**
 * 
 * This is the main interface to query the repository objects by index. 
 * 
 * @author Diego Miranda
 *
 */
public interface QueryCriteria extends Serializable {

	/**
	 * Sets the where clause of the query.
	 */
	public QueryCriteria where(Expression expression);
	
	/**
	 * Sets the field to order the result.
	 * @param field the field to order by.
	 */
	public QueryCriteria orderBy(String field);
	
	/**
	 * Sets the field to order the result.
	 * @param field the field to order by.
	 * @param reverse true to order in reverse mode.
	 */
	public QueryCriteria orderBy(String field, boolean reverse);
	
	/**
	 * Sets the page to retrieve.
	 * 
	 * @param page the number of the page to return.
	 */
	public QueryCriteria page (int page);
	
	/**
	 * Indicates that the query will retrieve data starting at this position.
	 */
	public QueryCriteria startAt (int position);
	
	/**
	 * Sets the amount of objects to be returned by query.
	 */
	public QueryCriteria maxResults (int maxResults);
	
	/**
	 * Returns the page obtained.
	 */
	public int getPage ();
	
	/**
	 * Returns the position of the retrieved data.
	 */
	public int getStartAt ();
	
	/**
	 * Returns the amount of object this query should return.
	 */
	public int getMaxResults ();
	
	/**
	 * Returns the Class associated to this query.
	 */
	public Class<?> getCriteriaClass();
	
	/**
	 * Returns the Lucene Sort Fields.
	 */
	public SortField[] getSortFields();
	
	/**
	 * Returns the list of Expressions used by this query.
	 */
	public List<Expression> getExpressions();
	public boolean isCheckForIndex();
	public QueryCriteria checkForIndex(boolean checkForIndex);
	
	/**
	 * Returns the Cache Id. This method is used internally by NoDB.
	 */
	public String getQueryCacheId ();
	
	/**
	 * Return true if this QueryCriteria should find objects strictly by the 
	 * assciated class.
	 * 
	 *  @see Repository.createCriteria
	 */
	public boolean isStricted();
	
	/**
	 * Find objects by index. It is mandatory to have an index created in order
	 * to locate the objects. If there is no index created or if you are querying
	 * an attribute that is not indexed, NDB will create a temporary index for 
	 * that query (this is by default). If you are working with a COMPRESSED repository,
	 * is recommended to disable the creation of temporary indexes to avoid loss in
	 * performance.
	 * 
	 * @see RepositoryConfiguration.setUseTemporalIndex
	 */
	public Result find ();
	
	/**
	 * Find objects by index.
	 * 
	 * @param where the where clause. This is useful when you have to iterate over
	 * the same query with diferent values.
	 * 
	 * @return a Result with the tuples and query information.
	 * @see RepositoryConfiguration.setUseTemporalIndex
	 */
	public Result find (Where where);
	
	public String getExpressionString ();
}
